/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import Bean.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Yohanes Beny P
 */
@WebServlet(urlPatterns = {"/SimpanDataPengiriman"})
public class SimpanDataPengiriman extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ParseException {
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParseException ex) {
            Logger.getLogger(SimpanDataPengiriman.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String nama_peng = request.getParameter("nama_peng");
        String nama_pene = request.getParameter("nama_pene");
        String tlp_peng = request.getParameter("tlp_peng");
        String almt_peng = request.getParameter("almt_peng");
        String almt_pene = request.getParameter("almt_pene");
        String jenis_brg = request.getParameter("jenis_brg");
        String bobot = request.getParameter("bobot");
        String cab = request.getParameter("cab");
        String tgl = request.getParameter("tgl_kirim");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = null;
        try {
            date = sdf.parse(tgl);
        } catch (ParseException ex) {
            Logger.getLogger(SimpanDataPengiriman.class.getName()).log(Level.SEVERE, null, ex);
        }

        Barang_Konsumen brg = new Barang_Konsumen();
        brg.setBobot(bobot);
        brg.setIdKantorCabang(cab.toUpperCase());
        brg.setJenisbarang(jenis_brg);
        try {
            brg.setNoResi(brg.generateNoResi(date, jenis_brg, cab));
        } catch (SQLException ex) {
            Logger.getLogger(SimpanDataPengiriman.class.getName()).log(Level.SEVERE, null, ex);
        }

        brg.setOngkos(String.valueOf(brg.hitungOngkos(Integer.parseInt(jenis_brg), Double.valueOf(bobot))));
        brg.setStatus("0");
        brg.setTanggalKirim(sdf.format(date));
        try {
            brg.terimaData(brg);
        } catch (SQLException ex) {
            Logger.getLogger(SimpanDataPengiriman.class.getName()).log(Level.SEVERE, null, ex);
        }
        Konsumen ksm = new Konsumen();
        ksm.setAlmt_penerima(almt_pene);
        ksm.setAlmt_pengirim(almt_peng);
        try {
            ksm.setId(String.valueOf(ksm.generateID()));
        } catch (SQLException ex) {
            Logger.getLogger(SimpanDataPengiriman.class.getName()).log(Level.SEVERE, null, ex);
        }
        ksm.setNama_penerima(nama_pene);
        ksm.setNama_pengirim(nama_peng);
        try {
            ksm.setNo_resi(brg.generateNoResi(date, jenis_brg, cab));
        } catch (SQLException ex) {
            Logger.getLogger(SimpanDataPengiriman.class.getName()).log(Level.SEVERE, null, ex);
        }

        ksm.setTlp_pengirim(tlp_peng);
        try {
            ksm.terimaKonsumen(ksm);
        } catch (SQLException ex) {
            Logger.getLogger(SimpanDataPengiriman.class.getName()).log(Level.SEVERE, null, ex);
        }


        out.println("<html>");
        out.println("<head>");
        out.println("<title>Konfirmasi Terima Barang</title>");
        out.println("<link href=\"css/style.css\" rel=\"stylesheet\" type=\"text/css\"/>");
        out.println("<script type=\"text/javascript\" src=\"js/base64.js\"></script>");
        out.println("<script type=\"text/javascript\" src=\"js/sprintf.js\"></script>");
        out.println("<script type=\"text/javascript\" src=\"js/jspdf.js\"></script>");

        out.println(" <script type=\"text/javascript\">");
        out.println("function demo1() {");
        out.println("var doc = new jsPDF();");
        out.println("doc.text(70, 20, 'BUKTI PENGIRIMAN BARANG');");
        out.println("doc.text(75, 30, 'PT. SRIWIJAYA EXPRESS');");
        out.println("doc.text(20, 40, '_____________________________________________________');");
        out.println("doc.setFontSize(10);");
        out.println("doc.text(20, 50, 'No. Resi');");
        out.println("doc.text(20, 60, 'Nama Pengirim');");
        out.println("doc.text(20, 70, 'Alamat Pengirim');");
        out.println("doc.text(20, 80, 'Nama Penerima');");
        out.println("doc.text(20, 90, 'Alamat Penerima');");
        out.println("doc.text(20, 100, 'Tujuan');");
        out.println("doc.text(20, 110, 'Biaya');");
        out.println("doc.text(70, 50, ':    " + ksm.getNo_resi() + "');");
        out.println("doc.text(70, 60, ':    " + ksm.getNama_pengirim() + "');");
        out.println("doc.text(70, 70, ':    " + ksm.getAlmt_pengirim() + "');");
        out.println("doc.text(70, 80, ':    " + ksm.getNama_penerima() + "');");
        out.println("doc.text(70, 90, ':    " + ksm.getAlmt_penerima() + "');");
        out.println("doc.text(70, 100, ':   " + brg.getIdKantorCabang() + "');");
        out.println("doc.text(70, 110, ':   " + brg.getOngkos() + "');");
        out.println("doc.output('datauri');");
        out.println("}");
        out.println("</script>");

        out.println("</head>");
        out.println("<body id=\"home\">");
        out.println("<div id=\"wrapper\">");
        out.println("<center>");
        out.println("</br>");
        out.println("<center><img src=\"image/header.png\" width=\"600\" height=\"100\"/></center>");
        out.println("<h1 align='right'> <a href=\"../HomeCabang.jsp\"><img src=\"image/btn_home.png\" width=\"100\" height=\"40\"/></a>");
        out.println(" <a href=\"../FilterLogout\"><img src=\"image/logout.png\" width=\"100\" height=\"40\"/></a></h1>");
        out.println("<table>");
        out.println("<tr>");
        out.println("<td align=\"left\"><h3><font color=\"black\">No. Resi</font></h3></td>");
        out.println("<td align=\"center\" width=\"5\"><h3><font color=\"black\">:</font></h3></td>");
        try {
            out.println("<td align=\"center\" width=\"5\"><h3><font color=\"black\">" + brg.generateNoResi(date, jenis_brg, cab) + "</font></h3></td>");
        } catch (SQLException ex) {
            Logger.getLogger(SimpanDataPengiriman.class.getName()).log(Level.SEVERE, null, ex);
        }
        out.println("</tr>");
        out.println("<tr>");
        out.println("<td align=\"left\"><h3><font color=\"black\">Nama Pengirim</font></h3></td>");
        out.println("<td align=\"center\" width=\"5\"><h3><font color=\"black\">:</font></h3></td>");
        out.println("<td align=\"center\" width=\"5\"><h3><font color=\"black\">" + ksm.getNama_pengirim() + "</font></h3></td>");
        out.println("</tr>");
        out.println("<tr>");
        out.println("<td align=\"left\"><h3><font color=\"black\">Alamat Pengirim</font></h3></td>");
        out.println("<td align=\"center\" width=\"5\"><h3><font color=\"black\">:</font></h3></td>");
        out.println("<td align=\"center\" width=\"5\"><h3><font color=\"black\">" + ksm.getAlmt_pengirim() + "</font></h3></td>");
        out.println("</tr>");
        out.println("<tr>");
        out.println("<td align=\"left\"><h3><font color=\"black\">Nama Penerima</font></h3></td>");
        out.println("<td align=\"center\" width=\"5\"><h3><font color=\"black\">:</font></h3></td>");
        out.println("<td align=\"center\" width=\"5\"><h3><font color=\"black\">" + ksm.getNama_penerima() + "</font></h3></td>");
        out.println("</tr>");
        out.println("<tr>");
        out.println("<td align=\"left\"><h3><font color=\"black\">Alamat Penerima</font></h3></td>");
        out.println("<td align=\"center\" width=\"5\"><h3><font color=\"black\">:</font></h3></td>");
        out.println("<td align=\"center\" width=\"5\"><h3><font color=\"black\">" + ksm.getAlmt_penerima() + "</font></h3></td>");
        out.println("</tr>");
        out.println("<tr>");
        out.println("<td align=\"left\"><h3><font color=\"black\">Tujuan</font></h3></td>");
        out.println("<td align=\"center\" width=\"5\"><h3><font color=\"black\">:</font></h3></td>");
        out.println("<td align=\"center\" width=\"5\"><h3><font color=\"black\">" + brg.getIdKantorCabang() + "</font></h3></td>");
        out.println("</tr>");
        out.println("<tr>");
        out.println("<td align=\"left\"><h3><font color=\"black\">Biaya</font></h3></td>");
        out.println("<td align=\"center\" width=\"5\"><h3><font color=\"black\">:</font></h3></td>");
        out.println("<td align=\"center\" width=\"5\"><h3><font color=\"black\">" + brg.getOngkos() + "</font></h3></td>");
        out.println("</tr>");
        out.println("</table>");
        out.println("</br></br>");
        out.println("<form action=\"HomeCabang.jsp\"><input type=\"submit\" name=\"submit\" value=\"Simpan\"/></form>");
        out.println("<form action=\"javascript:demo1();\"><input type=\"submit\" name=\"submit\" value=\"Cetak\"/></form>");
        out.println("</center>");
        out.println("</div>");
        out.println("</div>");
        out.println("</body>");
        out.println("</html>");
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
